-module(slots).
-include("slots.hrl").

%% -export([spin_slots/2, spin_slots2/2]).
-export([match_normal/1, match_any/2]).
-export([match_per_line/2]).
-export([pay_per_line/1]).

%% spin_slots(Wheel, Num) ->
%%     Temp = lists:map(fun(X) ->
%%                              {random:uniform(1 bsl 64), X}
%%                      end,
%%                      Wheel),
%%     Temp1 = lists:keysort(1, Temp),
%%     lists:map(fun(X) ->
%%                       element(2, X)
%%               end,
%%               Temp1).

%% spin_slots2(Wheel, Num) ->
%%     lists:map(fun(Loc) -> element(Loc, Wheel) end, myrandom:ran_list(size(Wheel), Num)).
%% %    generate_result(Wheel, size(Wheel), Num).


%% generate_result(_, _, Count) when Count == 0 ->
%%     [];
%% generate_result(Wheel, Size, Count) ->
%%     Random = random:uniform(Size),
%%     [element(Random, Wheel) | generate_result(Wheel, Size, spin_slots2Count - 1)].

pay_per_line([])->
    [];
pay_per_line([Match|T]) ->
    case Match of
        {?WILD, 5} -> Times = 2000;
        {?WILD, 4} -> Times = 500;
        {?WILD, 3} -> Times = 100;
        {?WILD, 2} -> Times = 25;

        {66, 5} -> Times = 1000;         % 'B'
        {66, 4} -> Times = 200;
        {66, 3} -> Times = 50;
        {66, 2} -> Times = 5;

        {67, 5} -> Times = 500;          % 'C'
        {67, 4} -> Times = 100;
        {67, 3} -> Times = 25;
        {67, 2} -> Times = 5;

        {68, 5} -> Times = 250;          % 'D'
        {68, 4} -> Times = 100;
        {68, 3} -> Times = 25;
        {68, 2} -> Times = 3;

        {69, 5} -> Times = 200;         % 'E'
        {69, 4} -> Times = 80;
        {69, 3} -> Times = 20;
        {69, 2} -> Times = 2;

        {70, 5} -> Times = 100;         % 'F'
        {70, 4} -> Times = 18;
        {70, 3} -> Times = 5;

        {71, 5} -> Times = 88;         % 'G'
        {71, 4} -> Times = 10;  
        {71, 3} -> Times = 3;

        {72, 5} -> Times = 28;         % 'H'
        {72, 4} -> Times = 8;
        {72, 3} -> Times = 3;

        {any, 5} -> Times = 8;
        {any, 4} -> Times = 5;
        {any, 3} -> Times = 2;
        _ -> Times = 0
    end,
    [{Match, Times} | pay_per_line(T)].
            

match_per_line(_, []) ->
    [];
match_per_line(Result, [MatchType|T]) ->
    lists:append(match_per_line(Result, MatchType), match_per_line(Result, T));

match_per_line(Result, {normal}) ->
    match_normal(Result);
match_per_line(Result, {any, AnySymbols}) ->
    match_any(Result, AnySymbols).

%% Finds the matching symbol and its count, return as [{symbol1, Count}, ...]
match_normal(Result) ->
    [Start | T] = Result,
    match_normal(Start, T, 1, []).

%% match_normal(Start, [Start| []], Count, Accu) ->
%%     [{Start, Count + 1} | Accu];
match_normal(Start, [Start| T], Count, Accu) ->
    match_normal(Start, T, Count + 1, Accu);
match_normal(?WILD, [Next| T], Count, Accu) ->
    match_normal(Next, T, Count + 1, [{?WILD, Count} | Accu]);
match_normal(Start, [?WILD| T], Count, Accu) ->
    match_normal(Start, T, Count + 1, Accu);
match_normal(Start, _, Count, Accu) ->
    [{Start, Count} | Accu].


match_any(Result, AnySymbols) ->
    [Start | T] = Result,
    case lists:member(Start, AnySymbols) of
        true -> match_any(T, 1, AnySymbols);
        _ -> [{any, 0, Start, AnySymbols}]
    end.

match_any([?WILD|T], Count, AnySymbols) ->
    match_any(T, Count+1, AnySymbols);

match_any([Next|T], Count, AnySymbols) ->
    case lists:member(Next, AnySymbols) of
        true -> match_any(T, Count+1, AnySymbols);
        _-> [{any, Count}]
    end;
match_any([], Count, _) ->
    [{any, Count}].

